package com.yyb.controller;

import com.yyb.annotation.AppPermission;
import com.yyb.annotation.Log;
import com.yyb.cache.CacheService;
import com.yyb.common.PageInfo;
import com.yyb.common.Result;
import com.yyb.domain.SysDictType;
import com.yyb.dto.SysDictTypeAddDTO;
import com.yyb.dto.SysDictTypeQueryDTO;
import com.yyb.dto.SysDictTypeUpdateDTO;
import com.yyb.enums.BusinessType;
import com.yyb.enums.ResultEnums;
import com.yyb.service.SysDictTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 
 * 
 * 
 */
@RestController
@RequestMapping("sysDictType")
@Tag(name = "字典类型相关的接口")
public class SysDictTypeController extends BaseController {

    @Resource
    private SysDictTypeService sysDictTypeService;

    @Resource
    private CacheService cacheService;

    /**
     * 分页查询字典类型数据
     * POST http://127.0.0.1:8080/sysDictType/getByPage
     * 接口ID：148742190
     * 接口地址：https://app.apifox.com/link/project/4040316/apis/api-148742190
     */
    @Operation(summary = "分页查询字典类型数据")
    @PostMapping("getByPage")
    @AppPermission(value = "system:dictType:query")
    public Result<PageInfo<SysDictType, SysDictTypeQueryDTO>> getByPage(
            @RequestBody PageInfo<SysDictType, SysDictTypeQueryDTO> pageInfo) {
        PageInfo<SysDictType, SysDictTypeQueryDTO> info = this.sysDictTypeService.getByPage(pageInfo);
        return new Result<>(ResultEnums.SUCCESS, info);
    }

    /**
     * 添加字典类型
     * POST http://127.0.0.1:8080/sysDictType/save
     * 接口ID：148742191
     * 接口地址：https://app.apifox.com/link/project/4040316/apis/api-148742191
     */
    @Operation(summary = "添加字典类型")
    @PostMapping("save")
    @AppPermission(value = "system:dictType:add")
    @Log(title = "字典类型管理-添加",businessType = BusinessType.INSERT)
    public Result<?> save(@RequestBody @Validated SysDictTypeAddDTO sysDictTypeAddDTO) {
        int row = this.sysDictTypeService.insert(sysDictTypeAddDTO);
        return this.toAjax(row > 0);
    }

    /**
     * 根据ID查询字典类型
     * GET http://127.0.0.1:8080/sysDictType/get/{dictId}
     * 接口ID：148742192
     * 接口地址：https://app.apifox.com/link/project/4040316/apis/api-148742192
     */
    @GetMapping("get/{dictId}")
    @Operation(summary = "根据ID查询字典类型")
    @AppPermission(value = "system:dictType:query")
    public Result<SysDictType> get(@PathVariable Long dictId) {
        SysDictType sysDictType = this.sysDictTypeService.selectByPrimaryKey(dictId);
        return new Result<>(ResultEnums.SUCCESS, sysDictType);
    }

    /**
     * 修改字典类型
     * PUT http://127.0.0.1:8080/sysDictType/update
     * 接口ID：148742193
     * 接口地址：https://app.apifox.com/link/project/4040316/apis/api-148742193
     */
    @PutMapping("update")
    @Operation(summary = "修改字典类型")
    @AppPermission(value = "system:dictType:update")
    @Log(title = "字典类型管理-修改",businessType = BusinessType.UPDATE)
    public Result<?> update(@RequestBody @Validated SysDictTypeUpdateDTO sysDictTypeUpdateDTO) {
        int row = this.sysDictTypeService.updateByPrimaryKey(sysDictTypeUpdateDTO);
        return this.toAjax(row > 0);
    }

    /**
     * 删除字典类型
     * DELETE http://127.0.0.1:8080/sysDictType/delete/{dictId}
     * 接口ID：148742194
     * 接口地址：https://app.apifox.com/link/project/4040316/apis/api-148742194
     */
    @DeleteMapping("delete/{dictId}")
    @Operation(summary = "删除字典类型")
    @AppPermission(value = "system:dictType:remove")
    @Log(title = "字典类型管理-删除",businessType = BusinessType.DELETE)
    public Result<?> delete(@PathVariable Long dictId) {
        int row = this.sysDictTypeService.deleteByPrimaryKey(dictId);
        return this.toAjax(row > 0);
    }

    /**
     * 同步字典数据到缓存
     * GET http://127.0.0.1:8080/sysDictType/refreshCache
     * 接口ID：148742195
     * 接口地址：https://app.apifox.com/link/project/4040316/apis/api-148742195
     */
    @GetMapping("refreshCache")
    @Operation(summary = "同步字典数据到缓存")
    @AppPermission(value = "system:dictType:cache")
    @Log(title = "字典类型管理-同步缓存",businessType = BusinessType.OTHER)
    public Result<?> refreshCache() {
        this.cacheService.refreshCache();
        return new Result<>(ResultEnums.SUCCESS);
    }
}
